/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package homebudget;

import java.awt.Panel;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultKeyedValuesDataset;
import org.jfree.data.general.DefaultPieDataset;

/**
 *
 * @author KriT
 */
public class Diagram {

    public void createBarDiagram(String s, DataBase db, String mode) {
        float amounts[] = new float[31];
        SimpleDateFormat sdf = new SimpleDateFormat("MMM,''yy");
        SimpleDateFormat day = new SimpleDateFormat("dd");


        if (mode == "expenses") {
            for (Entry e : db.expenses) {
                String date = sdf.format(e.date);
                if (date.equals(s)) {
                    date = day.format(e.date);
                    int index = Integer.parseInt(date);
                    amounts[index] += e.wartosc;//e.price * e.quantity;
                }
            }
        } else {
            for (Entry e : db.contributions) {
                String date = sdf.format(e.date);
                if (date.equals(s)) {
                    date = day.format(e.date);
                    int index = Integer.parseInt(date);
                    amounts[index] += e.wartosc;//e.price * e.quantity;
                }
            }
        }

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        for (int i = 0; i < amounts.length; i++) {
            dataset.addValue(amounts[i], "Suma", String.valueOf(i + 1));
        }

        JFreeChart barChart = ChartFactory.createBarChart("Podzial na dni", "Dzien", "Suma", dataset, PlotOrientation.VERTICAL, true, true, true);
        
        try {
            ChartUtilities.saveChartAsPNG(new File("chart.png"), barChart, 900, 500);
        } catch (IOException ex) {
            Logger.getLogger(Diagram.class.getName()).log(Level.SEVERE, null, ex);
        }

        JFrame frame = new JFrame("Display image");
        Panel panel = new ShowImage();
        frame.getContentPane().add(panel);
        frame.setSize(900, 500);
        frame.setVisible(true);
    }

    public void createPieChart(String s, DataBase db, Categories cat, String mode) {
        float[] amounts;
        SimpleDateFormat sdf = new SimpleDateFormat("MMM,''yy");

        if (mode == "expenses") {
            amounts = new float[cat.expenseCategories.size()];

            for (Entry e : db.expenses) {
                String date = sdf.format(e.date);
                if (date.equals(s)) {
                    int index = cat.expenseCategories.indexOf(e.category);
                    amounts[index] += e.wartosc;// e.price * e.quantity;
                }
            }
        } else {
            amounts = new float[cat.contributionCategories.size()];

            for (Entry e : db.contributions) {
                String date = sdf.format(e.date);
                if (date.equals(s)) {
                    int index = cat.contributionCategories.indexOf(e.category);
                    amounts[index] += e.wartosc;//e.price * e.quantity;
                }
            }
        }

        DefaultPieDataset pieData = new DefaultKeyedValuesDataset();

        for (int i = 0; i < amounts.length; i++) {
            String temp = new String();
            if (mode == "expenses") {
                temp = cat.expenseCategories.get(i);
            } else {
                temp = cat.contributionCategories.get(i);

            }

            pieData.setValue(temp, amounts[i]);
        }

        JFreeChart pieChart = ChartFactory.createPieChart("Podzial na kategorie", pieData, true, true, Locale.ENGLISH);
        try {
            ChartUtilities.saveChartAsPNG(new File("chart.png"), pieChart, 500, 500);
        } catch (IOException ex) {
            Logger.getLogger(Diagram.class.getName()).log(Level.SEVERE, null, ex);
        }

        JFrame frame = new JFrame("Display image");
        Panel panel = new ShowImage();
        frame.getContentPane().add(panel);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}
